<?php

class Str
{

    static public function checkStr($string, $type, $field)
    {
        global $lang;
        $variable = 'err_' . $field;
        $GLOBALS[$variable] = '';
        switch ($type)
        {
            case '@':
                if (!preg_match('#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#si', $string)) {
                    $GLOBALS[$variable] = fl('err_email');
                }
                break;
            case 'a':
                if (!preg_match('#^[a-zA-Z]+$#', $string)) {
                    $GLOBALS[$variable] = fl('err_a');
                }
                break;
            case '0':
                if (!preg_match('#^\d+$#', $string)) {
                    $GLOBALS[$variable] = fl('err_0');
                }
                break;
            case 'a0':
                if (!preg_match('#^[a-zA-Z0-9]+$#', $string)) {
                    $GLOBALS[$variable] = fl('err_a0');
                }
                break;
            case 'a1':
                if (!preg_match('#^[a-zA-Z0-9_-]+$#', $string)) {
                    $GLOBALS[$variable] = fl('err_a1');
                }
                break;
        }
        unset($variable);
    }

    static public function formatJs($string)
    {
        $string = preg_replace('#(\r\n|\r|\n)#', '<br />', $string);
        $string = str_replace(array('"', "'"), array('&quot;', '&#39;'));
        return $string;
    }

    static public function s2n($string)
    {
        return htmlspecialchars(trim($string));
    }

    static public function addlinks($value){
        $value = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
            "<a href=\"\\0\">\\0</a>", $value);
        return $value;
    }

    static public function getWData($url, $t_start, $t_end, $opt) {
        $pos_start = -1;
        $pos_end = -1;
        $gotdata = '';
        $handle = @fopen($url, "rb");
        $data = '';
        if ($handle){
            while (!feof($handle)) {
                $data .= fread($handle, 8192);
                if (($pos_start == -1) || ($pos_start === false)) {
                    $pos_start = strpos($data, $t_start);
                    if (($pos_start > -1) && ($pos_start != false)) {
                        $data = substr($data, $pos_start);
                    }
                }
                elseif (($pos_end == -1) || ($pos_end === false)) {
                    $pos_end = strpos($data, $t_end);
                    if (($pos_end > -1) && ($pos_end != false)) {
                        break;
                    }
                }
                if (($pos_start > -1) && ($pos_end > -1)) {
                    if ($opt == 1) {
                        $gotdata = substr($data, 0, $pos_end + strlen($t_end));
                    }
                    elseif ($opt == 2) {
                        $gotdata = substr($data, strlen($t_start), ($pos_end - strlen($t_start)));
                    }
                }
                fclose($handle);
                return $gotdata;
            }
        }
        else
            return "";

    }

    /**
     * Generates random string
     * @param integer $outputLength output string length including prefix
     * @param string $prefix
     * @param string $source source characters to take
     * @return string
     */
    static public function genRandom($outputLength = 0, $prefix = '', $source = '')
    {
        $output = '';
        $prefixLength = strlen($prefix);

        if ($outputLength < $prefixLength) {
            $outputLength = $prefixLength;
        }

        if ('' === $source) {
            $source = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
        }

        $maxIndex = strlen($source) - 1;
        while (strlen($output) < $outputLength)
        {
            $output .= $source[rand(0, $maxIndex)];
        }

        return $output;
    }

    static public function getTodayVN()
    {
        $output = '';
        $today = getdate();
        switch ($today['wday'])
            {
                case 1: 	//-------------------------
                    $output = 'Thứ Hai';
                    break;
                case 2: 	//-------------------
                    $output = 'Thứ Ba';
                    break;
                case 3: 	//-------------------
                    $output = 'Thứ Tư';
                    break;
                case 4: 	//-------------------
                    $output = 'Thứ Năm';
                    break;
                case 5: 	//-------------------
                    $output = 'Thứ Sáu';
                    break;
                case 6: 	//-------------------
                    $output = 'Thứ Bảy';
                    break;
                case 0: 	//-------------------
                    $output = 'Chủ Nhật';
                    break;
                default:
                    return 'Unknown day!';
            }

            return $output;
    }

}